import {createRouter, createWebHistory} from 'vue-router'
import {useUserStore} from "@/stores/user";
import {ElMessage} from "element-plus";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '系统首页',
      component:() => import('@/views/home/userHome.vue'),
      redirect:'/tianyanCommunity',
      children:[
        {path: '/tianyanCommunity',name:'天眼社区',component:()=>import('@/views/component/tianyanCommunity.vue')},
        {path: '/tianyanUpload',name:'天眼寻人',component:()=>import('@/views/component/tianyanUpload.vue')}
      ]
    },
    {
      path:'/login',
      name:'login',
      component:()=>import('@/views/Login.vue'),
    },
    {
      path: '/register',
      name:'register',
      component: () => import('@/views/Register.vue'),
    },
    // {
    //   path: '/ADMIN',
    //   name: '系统管理页面',
    //   component: () => import('@/views/manager/Manager.vue'),
    //   redirect:'/ADMIN/home',
    //   meta: {
    //     requireAuth: true,
    //     roles: ['ADMIN','USER']
    //   },
    //   children: [
    //     { path: '/ADMIN/updateInfo', component: () => import('@/views/manager/updateInfo.vue')},
    //     { path: '/ADMIN/password', component: () => import('@/views/manager/Password.vue')},
    //     { path: '/ADMIN/home', component: () => import('@/views/manager/Home.vue')},
    //     { path: '/ADMIN/user', component: () => import('@/views/manager/UserManager.vue')},
    //     {path: '/ADMIN/person',component:()=>import('@/views/manager/person.vue')},
    //   ]
    // },
    // {
    //   path: '/ORDINARY-USER',
    //   name: '普通用户界面',
    //   component: () => import('@/views/manager/Manager.vue'),
    //   meta: {
    //     requireAuth: true,
    //     roles: ['USER']
    //   },
    //   redirect: '/ORDINARY-USER/home',
    //   children: [
    //     { path: '/ORDINARY-USER/person', component: () => import('@/views/manager/updateInfo.vue')},
    //     { path: '/ORDINARY-USER/password', component: () => import('@/views/manager/Password.vue')},
    //     { path: '/ORDINARY-USER/home', component: () => import('@/views/manager/Home.vue')},
    //     { path: '/ORDINARY-USER/admin', component: () => import('@/views/manager/UserManager.vue')},
    //   ]
    // },
    {
      path: '/user',
      name: 'userCenter',
      redirect:'/user/profile',
      component: () => import('@/views/userCenter/userCenter.vue'),
      meta: {
        requireAuth: true,
        roles: ['USER','ADMIN','POLICE']
      },
      children:[
        { path: '/user/profile',name:'userProfile', component: () => import('@/views/userCenter/profile.vue')},
        { path: '/user/password', component: () => import('@/views/userCenter/changePassword.vue')},
        { path: '/user/message', component: () => import('@/views/userCenter/myMessages.vue')},
        { path: '/user/post', component: () => import('@/views/userCenter/myPost.vue')}
      ]
    },
    {
      path: '/admin',
      name: 'adminCenter',
      component: () => import('@/views/admin/adminCenter.vue'),
      meta: {
        requireAuth: true,
        roles: ['ADMIN']
      },
    },
    {
      path:'/post/:nid',
      name:'postDetail',
      component:()=>import('@/views/detail/missingDetail.vue'),
    },
    {
      path:'/search/',
      name:'search',
      component:()=>import('@/views/common/searchResult.vue'),
    },
    {
      path: '/policePage',
      name: 'policePage',
      meta: {
        requireAuth: true,
        roles: ['POLICE']
      },
      component:() => import('@/views/police/policePage.vue'),
      redirect:'/policePage/missing',
      children:[
        { path: '/policePage/missing', component: () => import('@/views/police/missing.vue')},
        { path: '/policePage/video', component: () => import('@/views/police/video.vue')},
        { path: '/policePage/clue', component: () => import('@/views/police/clue.vue')},
        ]
    },
    {
      //测试接口页面
      path:'/test',
      name:'testPage',
      component:()=>import('@/views/Test.vue'),
    },
    // 不识别的path自动匹配404
    {path:'/:catchAll(.*)',name:'NotFind',component:() =>import('@/views/common/NotFind.vue')},

  ]
})

router.beforeEach((to, from, next) => {
  let isLogin=useUserStore().getToken()
  let role=isLogin?useUserStore().getUserInfo().role:null
  // 判断该路由是否需要登录权限
  if (to.meta.requireAuth) {
    // 如果需要，则校验用户是否已经登录
    if (isLogin) {
      // 判断当前用户是否有访问该路由的权限
      if (to.meta.roles.includes(role)) {
        next() // 用户有访问权限，直接进入页面
      } else {
        ElMessage.error('当前用户无权限访问此页面！')
        next('/')
      }
    } else {
      ElMessage.error('请先登录')
      next('/login')
    }
  } else {
    next() // 如果不需要登录权限，直接进入页面
  }
})

export default router
